package com.young.mylib.db.dao;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.young.mylib.db.DatabaseUtils;

import com.young.mylib.db.entity.ActivationConditionEntity;
import com.young.mylib.db.entity.ActivationEntity;
import com.young.mylib.db.entity.FailpointEntity;
import com.young.mylib.db.entity.FailpointStatusEntity;
import com.young.mylib.utils.FastJsonUtils;

import java.util.List;

/**
 * Created by Young on 2018/5/21.
 */

public class MtbfDao extends BaseDao {
    private final static String TAG = "MtbfDao";

    public static int getActivationTime() {
        List<ActivationEntity> list = DatabaseUtils.getHelper().queryAll(ActivationEntity.class);
        if (list == null) return 0;
        if (list.size() == 0) return 0;
        return (int)list.get(0).activationTime;
    }

    public static void setActivationTime(String devid, int time) {
        ActivationEntity entity = new ActivationEntity();
        List<ActivationEntity> list = DatabaseUtils.getHelper().queryAll(ActivationEntity.class);
        if (list == null || list.size() == 0) {
            entity = new ActivationEntity();
        } else {
            entity = list.get(0);
        }
        entity.activationTime = time;
        DatabaseUtils.getHelper().updateByKey(entity, "devId", devid);
    }

    public static void updateActivation(ActivationConditionEntity entity) {
        if (entity == null) return;
        DatabaseUtils.getHelper().updateByKey(entity, "devId", entity.deviceId);
    }

    public static ActivationConditionEntity getActivation(String id) {
        List<ActivationConditionEntity> list = DatabaseUtils.getHelper().queryAll(ActivationConditionEntity.class);
        if (list == null) return null;
        if (list.size() == 0) return null;
        for (ActivationConditionEntity v : list) {
            if (v.deviceId.equals(id)) return v;
        }
        return null;
    }

    public static JSONObject updateFailpointStatus(FailpointStatusEntity entity) {
        /**
         * 点停开始
         */
        if (entity.status == 0) {
            DatabaseUtils.getHelper().save(entity);
            JSONObject json = DatabaseUtils.getHelper().queryOneByKey(FailpointEntity.class, "item", entity.item);
            if (json == null) return null;
            return json;
        }
        /**
         * 点停结束
         */
        JSONObject json = DatabaseUtils.getHelper().queryOneByKey(FailpointStatusEntity.class, "item", entity.item);

        if (json == null) {
            Log.i(TAG, "json is null");
            return null;
        }
        FailpointStatusEntity statusEntity = FastJsonUtils.toBean(json.toString(), new FailpointStatusEntity().getClass());
        int second = (int) ((entity.time - statusEntity.time) / 1000);
        System.out.println("second: " + second);
        DatabaseUtils.getHelper().deleteByKey(FailpointStatusEntity.class, "item", entity.item);

        return updateFailpoint(entity.item, second+"");
    }

    private static JSONObject updateFailpoint(String item, String timeLength) {
        JSONObject json = DatabaseUtils.getHelper().queryOneByKey(FailpointEntity.class, "item", item);
        if (json == null) return null;
        FailpointEntity entity = FastJsonUtils.toBean(json.toString(), new FailpointEntity().getClass());
        if (entity == null) return null;
        entity.count = entity.count + 1;
        entity.timeLength = timeLength;
        DatabaseUtils.getHelper().updateByKey(entity, "item", item);
        return FastJsonUtils.toJsonObject(entity);
    }

    public static List<FailpointEntity> getFailpoints() {
        return DatabaseUtils.getHelper().queryAll(FailpointEntity.class);
    }

    public static void clear() {
        DatabaseUtils.getHelper().clear(FailpointEntity.class);
        DatabaseUtils.getHelper().clear(FailpointStatusEntity.class);
    }

    /**
     * 检查指定的项目是否存在
     *
     * @param item
     * @return
     */
    public static boolean itemIsExist(String item) {
        return valueIsExist("item", item);
    }

    public static boolean valueIsExist(String key, String value) {
        List<FailpointEntity> list = DatabaseUtils.getHelper().queryByKey(FailpointEntity.class, key, value);
        if (list == null) return false;
        if (list.size() == 0) return false;
        return true;
    }

    private static int getCount(String key, String value) {
        List<FailpointEntity> list = DatabaseUtils.getHelper().queryByKey(FailpointEntity.class, key, value);
        if (list == null) return 0;
        return list.size();

    }

}
